package com.aistar.task;

import com.aistar.mapper.ChoiceCollectMapper;
import com.aistar.pojo.ChoiceCollect;
import com.aistar.pojo.ChoiceCollectExample;
import com.aistar.util.MessageUtil;
import com.aistar.util.RedisMessage;
import com.aistar.util.RedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
public class ChoiceRedisTask {
    @Autowired
    private ChoiceCollectMapper choiceCollectMapper;
    private Log log = LogFactory.getLog(ChoiceRedisTask.class);

    @Scheduled(cron = "5/5 * * * * ?") // 每5s运行一次
    //@Scheduled(cron = "* 5/5 * * * ?") // 每5min运行一次
    private void runCollect(){
        int count=0;
        // 将redis数据持久化到数据库中  map里面的参数为（field和value）
        Map<Object,Object> map= RedisUtil.hgetAll(RedisMessage.CHOICE_COLLECT_KEY);
        for(Map.Entry<Object,Object> entry:map.entrySet()){
            String[] fields=((String)entry.getKey()).split(RedisMessage.SEPARATOR);
            Integer customerId=Integer.valueOf(fields[0]);
            Integer choiceId=Integer.valueOf(fields[1]);

            String[] values=((String)entry.getValue()).split(RedisMessage.SEPARATOR);
            int status=Integer.valueOf(values[0]);
            Date collectTime=new Date(Long.valueOf(values[1]));

            ChoiceCollectExample example=new ChoiceCollectExample();
            example.createCriteria().andChoiceIdEqualTo(choiceId)
                    .andCustomerIdEqualTo(customerId);
            List<ChoiceCollect> choiceCollectList=choiceCollectMapper.selectByExample(example);

            ChoiceCollect choiceCollect;

            if(choiceCollectList !=null && choiceCollectList.size()>0){//当数据库中有数据
                if(choiceCollectList.get(0).getStatus() !=status){
                    choiceCollect=choiceCollectList.get(0);
                    choiceCollect.setStatus(status);
                    choiceCollect.setChoiceCollectTime(collectTime);
                    count +=choiceCollectMapper.updateByPrimaryKeySelective(choiceCollect);
                }
            }else if(MessageUtil.STATUS_FAIL != status){//当数据库里面没有数据时，且点击了收藏时
                choiceCollect=new ChoiceCollect();
                choiceCollect.setCustomerId(customerId);
                choiceCollect.setChoiceId(choiceId);
                choiceCollect.setChoiceCollectTime(collectTime);
                count +=choiceCollectMapper.insertSelective(choiceCollect);
            }
            //清空Redis里面的缓存
            RedisUtil.hdel(RedisMessage.CHOICE_COLLECT_KEY,(String)entry.getKey());
        }

        if(count != 0)
            log.info("redis choiceCollect change successful rows: " + count);

    }

}
